﻿// ***********************************************************************
// Assembly         : OSS.BIM
// Author           : TianTeng
// Created          : 09-03-2018
//
// Last Modified By : TianTeng
// Last Modified On : 09-03-2018
// ***********************************************************************
// <copyright file="ModelGropeDbUtil.cs" company="北京智帆高科科技有限公司">
//     Copyright © 北京智帆高科科技有限公司 2018
// </copyright>
// <summary></summary>
// ***********************************************************************

using System.Collections.Generic;
using OSS.BIM.Model;
using ZFGK.Utility;

namespace OSS.BIM.Business.Utility
{
    /// <summary>
    /// Class ModelGropeDbUtil.
    /// </summary>
    public static class ModelGropeDbUtil
    {
        /// <summary>
        /// Gets at.
        /// </summary>
        /// <param name="modelGropeId">The model grope identifier.</param>
        /// <returns>ModelGrope.</returns>
        public static ModelGrope GetAt(long modelGropeId)
        {
            return DB.Context.From<ModelGrope>().Where(d => d.ModelGropeId == modelGropeId).First();
        }

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <param name="project">The modelGrope.</param>
        /// <returns>ModelGrope[].</returns>
        public static ModelGrope[] GetModelGropes(this Project project)
        {
            return DB.Context.From<ModelGrope>().Where(d => d.ProjectId == project.ProjectId).ToList().ToArray();
        }

        /// <summary>
        /// News the identifier if not set.
        /// </summary>
        /// <param name="modelGrope">The model grope.</param>
        public static void NewIdIfNotSet(this ModelGrope modelGrope)
        {
            if (modelGrope.ModelGropeId == 0)
                modelGrope.ModelGropeId = SnowflakeUtil.GetId();
        }

        /// <summary>
        /// Inserts the specified modelGrope.
        /// </summary>
        /// <param name="modelGrope">The modelGrope.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
        public static bool Insert(ModelGrope modelGrope)
        {
            modelGrope.NewIdIfNotSet();
            return DB.Context.Insert(modelGrope) > 0;
        }

        /// <summary>
        /// Inserts the specified model gropes.
        /// </summary>
        /// <param name="modelGropes">The model gropes.</param>
        /// <returns>System.Boolean.</returns>
        public static bool Insert(IEnumerable<ModelGrope> modelGropes)
        {
            modelGropes.ForEach(d => d.NewIdIfNotSet());
            return DB.Context.Insert(modelGropes) > 0;
        }

        /// <summary>
        /// Gets the last identifier.
        /// </summary>
        /// <returns>System.Int32.</returns>
        public static long GetLastId()
        {
            return DB.Context.From<ModelGrope>().Select(d => d.ModelGropeId)
                .OrderByDescending(ModelGrope._.ModelGropeId).Top(1).First().ModelGropeId;
        }

        /// <summary>
        /// Gets the project.
        /// </summary>
        /// <param name="modelGrope">The model grope.</param>
        /// <returns>Project.</returns>
        public static Project GetProject(this ModelGrope modelGrope)
        {
            return ProjectDbUtil.GetAt(modelGrope.ProjectId);
        }

        /// <summary>
        /// Determines whether the specified user group is exit.
        /// </summary>
        /// <param name="userGroup">The user group.</param>
        /// <returns>System.Int32.</returns>
        public static int IsExit(this ModelGrope userGroup)
        {
            return DB.Context.From<ModelGrope>().Where(d => d.ModelGropeId == userGroup.ModelGropeId).Count();
        }

        /// <summary>
        /// Gets the first user group.
        /// </summary>
        /// <param name="userProject">The user project.</param>
        /// <returns>UserGroup.</returns>
        public static ModelGrope GetFirstUserGroup(this Project userProject)
        {
            return DB.Context.From<ModelGrope>().Where(d => d.ProjectId == userProject.ProjectId).First();
        }
    }
}
